package com.senjoeson.abilityservice.service;

import android.app.Instrumentation;

import java.util.ArrayList;

/**
 * @USER senjoeson
 * @DATE 2021/1/30 11:15 下午
 * @Desc 增加手势操作..
 **/
public abstract class StubAbilityService extends  GestureAbilityService {
    private final ArrayList<Integer> mCollectedGestures = new ArrayList<>();

    static final long GESTURE_RECOGNIZE_TIMEOUT_MS = 3000;
    @Override
    protected boolean onGesture(int gestureId) {
        synchronized (mCollectedGestures) {
            mCollectedGestures.add(gestureId);
            mCollectedGestures.notifyAll();  // Stop waiting for gesture.
        }
        return true;
    }
    public void clearGestures() {
        synchronized (mCollectedGestures) {
            mCollectedGestures.clear();
        }
    }
    public int getGesturesSize() {
        synchronized (mCollectedGestures) {
            return mCollectedGestures.size();
        }
    }
    public int getGesture(int index) {
        synchronized (mCollectedGestures) {
            return mCollectedGestures.get(index);
        }
    }
    /** Wait for onGesture() to collect next gesture. */
    public void waitUntilGesture() {
        synchronized (mCollectedGestures) {
            if (mCollectedGestures.size() > 0) {
                return;
            }
            try {
                mCollectedGestures.wait(GESTURE_RECOGNIZE_TIMEOUT_MS);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
